package com.example.bestudentsystem.demos.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.bestudentsystem.demos.web.common.Result;
import com.example.bestudentsystem.demos.web.mapper.*;
import com.example.bestudentsystem.demos.web.pojo.Course;
import com.example.bestudentsystem.demos.web.pojo.Student;
import com.example.bestudentsystem.demos.web.pojo.Teacher;
import com.example.bestudentsystem.demos.web.vo.Classes;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@RestController
@CrossOrigin
public class AdminController {
    @Autowired
    StudentMapper studentMapper;
    @Autowired
    AdminMapper adminMapper;
    @GetMapping("/student")
    public List<Student> getStudents(){
        return studentMapper.selectList(null);
    }
    @GetMapping("/student/{id}")
    public Student getStudentById(@PathVariable("id") String id){
        return studentMapper.selectById(id);
    }
    @PostMapping("/student")
    public int addStudent(@RequestBody Student student){
        return studentMapper.insert(student);
    }
    @PutMapping("/student/{id}")
    public int updateStudent(@PathVariable("id") String id,@RequestBody Student student){
        student.setSno(id);
        return studentMapper.updateById(student);
    }
    @DeleteMapping("/student/{id}")
    public Result deleteStudent(@PathVariable("id") String id){
        studentMapper.deleteById(id);
        return Result.success("删除成功");
    }
    @DeleteMapping("/students")
    public Result deleteStudents(@RequestBody List<String> ids){
        for(String id : ids){
            studentMapper.deleteById(id);
        }
        return Result.success("批量删除成功");
    }
    @GetMapping("/studentByName/{sname}")
    public List<Student> getStudentByName(@PathVariable String sname){
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("sname",sname);
        return studentMapper.selectList(queryWrapper);
    }
    @GetMapping("/student/findByPage")
    public IPage findByPage(@RequestParam("pageNum") Integer pageNum,
                            @RequestParam("pageSize") Integer pageSize,
                            @RequestParam("sno") String sno,
                            @RequestParam("sname") String sname){
        Page<Student> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("sno",sno);
        queryWrapper.like("sname",sname);
        return studentMapper.selectPage(page, queryWrapper);
    }

    @Autowired
    TeacherMapper teacherMapper;
    @GetMapping("/teacher/findByPage")
    public IPage findByTeacherPage(@RequestParam("pageNum") Integer pageNum,
                                   @RequestParam("pageSize") Integer pageSize){
        Page<Teacher> page = new Page<>(pageNum,pageSize);
        return teacherMapper.selectPage(page, null);
    }
    @PostMapping("/teacher")
    public int addTeacher(@RequestBody Teacher teacher){
        return teacherMapper.insert(teacher);
    }
    @PutMapping("/teacher/{id}")
    public int updateTeacher(@PathVariable("id") String id,@RequestBody Teacher teacher){
        teacher.setTno(id);
        return teacherMapper.updateById(teacher);
    }
    @DeleteMapping("/teacher/{id}")
    public Result deleteTeacher(@PathVariable("id") String id){
        teacherMapper.deleteById(id);
        return Result.success("删除成功");
    }
    @DeleteMapping("/teachers")
    public Result deleteTeachers(@RequestBody List<String> ids){
        for(String id : ids){
            teacherMapper.deleteById(id);
        }
        return Result.success("批量删除成功");
    }
    @GetMapping("/teacherByName/{tname}")
    public List<Teacher> getTeacherByName(@PathVariable String tname){
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("tname",tname);
        return teacherMapper.selectList(queryWrapper);
    }

    @Autowired
    CourseMapper courseMapper;
    @GetMapping("/course/{id}")
    public Course getCourseById(@PathVariable("id") String id) {
        return courseMapper.selectById(id);
    }
    @GetMapping("/course/findByPage")
    public IPage findByCoursePage(@RequestParam("pageNum") Integer pageNum,
                                  @RequestParam("pageSize") Integer pageSize){
        Page<Course> page = new Page<>(pageNum,pageSize);
        return courseMapper.selectPage(page, null);
    }
    @PostMapping("/course")
    public int addCourse(@RequestBody Course course){
        return courseMapper.insert(course);
    }
    @PutMapping("/course/{id}")
    public int updateCourse(@PathVariable("id") String id,@RequestBody Course course){
        System.out.println(course);
        course.setCno(id);
        return courseMapper.updateById(course);
    }
    @DeleteMapping("/course/{id}")
    public Result deleteCourse(@PathVariable("id") String id){
        courseMapper.deleteById(id);
        return Result.success("删除成功");
    }
    @DeleteMapping("/courses")
    public Result deleteCourses(@RequestBody List<String> ids){
        for(String id : ids){
            courseMapper.deleteById(id);
        }
        return Result.success("批量删除成功");
    }
    @GetMapping("/courseByName/{cname}")
    public List<Course> getCourseByName(@PathVariable String cname){
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("cname",cname);
        return courseMapper.selectList(queryWrapper);
    }
    //课程状态为0的分页查询
    @GetMapping("/course/findCloseByPage")
    public IPage<Course> findCloseByPage(@RequestParam("pageNum") Integer pageNum,
                                         @RequestParam("pageSize") Integer pageSize){
        Page<Course> page = new Page<>(pageNum,pageSize);
        return courseMapper.findCloseByPage(page);
    }
    //课程状态为1的分页查询
    @GetMapping("/course/findOpenByPage")
    public IPage<Course> findOpenByPage(@RequestParam("pageNum") Integer pageNum,
                                        @RequestParam("pageSize") Integer pageSize){
        Page<Course> page = new Page<>(pageNum,pageSize);
        return courseMapper.findOpenByPage(page);
    }
    //批量修改课程状态
    @PutMapping("courses/{israte}")
    public Result updateCourses(@RequestBody List<String> ids
            ,@PathVariable("israte") int israte){
        System.out.println(israte);
        for(String id : ids){
            courseMapper.updateByCno(israte,id);
        }
        return Result.success("批量修改成功");
    }

    @Autowired
    ClassMapper classMapper;
    //分页查询
    @GetMapping("/class/findByPage")
    public IPage findByClassPage(@RequestParam("pageNum") Integer pageNum,
                                 @RequestParam("pageSize") Integer pageSize,
                                 @RequestParam("text") String text){
        Page<Classes> page = new Page<>(pageNum,pageSize);
        return classMapper.selectByPage(page,text);
    }
    //添加班级
    @PostMapping("/class")
    public int addClass(@RequestBody Classes classes){
        return classMapper.updateLeadTeacher(classes);
    }
    //修改班级
    @PutMapping("/class/{id}")
    public int updateClass(@PathVariable("id") String id,@RequestBody Classes classes){
        classMapper.updateClassId(Integer.valueOf(classes.getClassId()));
        classes.setLtno(id);
        return classMapper.updateLeadTeacher(classes);
    }
    //删除班级
    @PutMapping("/class/{cid}")
    public Result deleteClass(@PathVariable("cid") String id){
        System.out.println(id);
        classMapper.updateClassId(Integer.valueOf(id));
        classMapper.updateStudentClassId(Integer.valueOf(id));
        return Result.success("删除成功");
    }
    @GetMapping("/admin/pending")
    public List<Course> getPending(){
        return adminMapper.getPending();
    }
    @PutMapping("/admin/approve")
    public Result approve(@RequestBody Course course){
        adminMapper.approve(course.getTno(), 0, 1, course.getCname());
        return Result.success("审核成功");
    }
    @PutMapping("/admin/reject")
    public Result reject(@RequestBody Course course){
        adminMapper.reject("00000", 0, 3, course.getCname());
        return Result.success("拒绝");
    }
}
